package com.ws.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ws.bean.User;
import com.ws.common.CommonResult;
import com.ws.service.UserService;
import com.ws.util.Page;

@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
	UserService service;
	@GetMapping("select")
	public CommonResult getUser(User user , Page page) {
		List<User> list = service.getUser(user,page);
		int count = service.getCount(user);
		Map map = new HashMap<>();
		map.put("list", list);
		map.put("count", count);
		return CommonResult.success(map);
	}
	@DeleteMapping("del/{id}")
	public CommonResult delUser(@PathVariable Integer id) {
		int result = service.delUser(id);
		return CommonResult.success(result);
	}
	@PutMapping("edit")
	public CommonResult editUser(User user){
		int result = service.editUser(user);
		return CommonResult.success();
	}
	@PostMapping("add")
	public CommonResult addUser(@RequestBody User user) {
		int result = service.addUser(user);
		return CommonResult.success(result);
	}
	
	@PutMapping("editpass")
	public CommonResult editPassword(User user) {
		//测试是否传入参数
//		System.out.println("传入的user是：");
//		System.out.println(user.getUsername());
//		System.out.println(user.getNewpassword());
//		System.out.println(user.getPassword());
		int result = service.editPssword(user);
		return CommonResult.success();
	}
	
}
